package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.example.entity.TImage;
import org.example.result.Result;
import org.example.service.TImageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>(TImage)表控制层
 *
 * @author makejava
 * @since 2022-11-04 13:50:19
 */
@RestController
@RequestMapping("tImage")
@Api(tags = "图片")
@RequiredArgsConstructor
public class TImageController {
    /**
     * 服务对象
     */
    private final TImageService tImageService;

    @Value("${website.image-url}")
    private String baseUrl;

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param tImage 查询实体
     * @return 所有数据
     */
    @GetMapping
    public Result<?> selectAll(Page<TImage> page, TImage tImage) {
        return Result.ok(this.tImageService.page(page, new QueryWrapper<>(tImage)));
    }

    @GetMapping("/getPhotoById")
    @ApiOperation("得到message的图片")
    public Result<?> getPhotoById(Long messageId) {
        ArrayList<String> photos = new ArrayList<>();
        for (TImage umsImage : tImageService.list(new LambdaQueryWrapper<TImage>().eq(TImage::getMessageId, messageId))) {
            photos.add(umsImage.getName());
        }

        return Result.ok(photos);
    }

    /**
     * 得到最新图片服务器地址
     */
    @GetMapping("getNewImage")
    public Result<?> getNewImage() {
        return Result.ok(baseUrl);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<?> selectOne(@PathVariable Serializable id) {
        return Result.ok(this.tImageService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tImage 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result<?> insert(@RequestBody TImage tImage) {
        return Result.ok(this.tImageService.save(tImage));
    }

    /**
     * 修改数据
     *
     * @param tImage 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result<?> update(@RequestBody TImage tImage) {
        return Result.ok(this.tImageService.updateById(tImage));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result<?> delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(this.tImageService.removeByIds(idList));
    }
}

